package com.asistencia.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.asistencia.dao.interfaces.ScheduleDao;
import com.asistencia.domain.Classroom;
import com.asistencia.domain.Course;
import com.asistencia.domain.Professor;
import com.asistencia.domain.Schedule;
@Repository
@SuppressWarnings({ "rawtypes", "unchecked" })

public class ScheduleJdbc implements ScheduleDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public int updateById(Schedule schedule) throws DataAccessException {
		String SQL = "UPDATE schedule SET state = ? WHERE idschedule = ?";
		int result;
		result = jdbcTemplate.update(SQL, new Object[] { schedule.getState(),schedule.getIdSchedule()});
		return result;
	}
	
	@Override
	public List<Schedule> listByCourseSemester(Integer idCourse,
			Integer idSemester) throws DataAccessException {
		String SQL ="SELECT * FROM schedule WHERE idsemester =? AND idcourse =? ";
		List matches = jdbcTemplate.query(SQL,new Object []{idSemester,idCourse }, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Schedule sche= new Schedule();
				Classroom clas=new Classroom();
				Course co=new Course();
				Professor pro= new Professor();
				sche.setIdSchedule(rs.getInt("idschedule"));
				sche.setDate(rs.getString("date"));
				sche.setStartTime(rs.getString("startime"));
				sche.setEndTime(rs.getString("endtime"));
				sche.setTolerance(rs.getInt("tolerance"));
				sche.setTopic(rs.getString("topic"));
				sche.setState(rs.getString("state"));
				clas.setIdClassroom(rs.getInt("idclassroom"));
				co.setIdCourse(rs.getInt("idcourse"));
				pro.setIdProfessor(rs.getInt("idprofessor"));
				sche.setMyProfessor(pro);
				sche.setMyCourse(co);
				sche.setMyClassroom(clas);
				return sche;
			}});
			return (matches.size() > 0 ? (List<Schedule>) matches : null);
	}
	@Override
	public List<Schedule> listByClassroomSemester(Integer idClassroom,
			Integer idSemester) throws DataAccessException {
		//ojooo
		String SQL ="SELECT * FROM schedule WHERE idClassroom = ? AND idSemester = ? ";
		List matches = jdbcTemplate.query(SQL,new Object []{idClassroom ,idSemester}, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Schedule sche= new Schedule();
				Classroom clas=new Classroom();
				Course co=new Course();
				Professor pro= new Professor();
				sche.setIdSchedule(rs.getInt("idschedule"));
				sche.setDate(rs.getString("date"));
				sche.setStartTime(rs.getString("startime"));
				sche.setEndTime(rs.getString("endtime"));
				sche.setTolerance(rs.getInt("tolerance"));
				sche.setTopic(rs.getString("topic"));
				sche.setState(rs.getString("state"));
				clas.setIdClassroom(rs.getInt("idclassroom"));
				co.setIdCourse(rs.getInt("idcourse"));
				pro.setIdProfessor(rs.getInt("idprofessor"));
				sche.setMyProfessor(pro);
				sche.setMyCourse(co);
				sche.setMyClassroom(clas);
				return sche;
			}});
			return (matches.size() > 0 ? (List<Schedule>) matches : null);
	}
		
	

	@Override
	public Schedule getById(Integer idSchedule) throws DataAccessException {
		String SQL ="SELECT * FROM schedule WHERE idschedule= ? LIMIT 1 ";
		List matches = jdbcTemplate.query(SQL, new Object[]{idSchedule}, new RowMapper(){
			
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Schedule sche= new Schedule();
				Classroom clas=new Classroom();
				Course co=new Course();
				Professor pro= new Professor();
				sche.setIdSchedule(rs.getInt("idschedule"));
				sche.setDate(rs.getString("date"));
				sche.setStartTime(rs.getString("startime"));
				sche.setEndTime(rs.getString("endtime"));
				sche.setTolerance(rs.getInt("tolerance"));
				sche.setTopic(rs.getString("topic"));
				sche.setState(rs.getString("state"));
				clas.setIdClassroom(rs.getInt("idclassroom"));
				co.setIdCourse(rs.getInt("idcourse"));
				pro.setIdProfessor(rs.getInt("idprofessor"));
				sche.setMyProfessor(pro);
				sche.setMyCourse(co);
				sche.setMyClassroom(clas);
				return sche;
			}});
		return (matches.size() > 0 ? (Schedule) matches.get(0) : null);
	}
		
}
